package com.cwiyc.orderonlineback.service;

import com.cwiyc.orderonlineback.common.Result;
import com.cwiyc.orderonlineback.entity.Shop;
import com.cwiyc.orderonlineback.mapper.ShopMapper;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import java.util.List;

@Service
public class ShopServiceImpl {
    @Resource
    ShopMapper shopMapper;

    public Result<?> getShop(@RequestBody Shop shop) {
        Shop res = shopMapper.getshop(shop.getSsixcode());
        if (res == null) {
            return Result.error("-1", "商家不存在");
        }
        return Result.success(res);
    }

    public Result<?> updateSpic(@RequestBody Shop shop) {
        int res = shopMapper.updatespic(shop.getSowner(), shop.getSpicture());
        if (res == 0) {
            return Result.error("-1", "更新店铺图片失败");
        }
        return Result.success();
    }

    public Result<?> updateSinfo(@RequestBody Shop shop) {
        int res = shopMapper.updatesinfo(shop.getSowner(), shop.getScreatetime(), shop.getSintroduction(), shop.getSname(), shop.getSstatus());
        if (res == 0) {
            return Result.error("-1", "更新店铺信息失败");
        }
        Shop ress = shopMapper.afterupsinfo(shop.getSowner());
        if (ress == null) {
            return Result.error("-1", "获取更新后店铺信息失败");
        }
        return Result.success(ress);
    }

    public Result<?> getCodebyuid(@RequestBody Shop shop) {
        String res = shopMapper.getcodebyuid(shop.getSuid());
        if (res == null) {
            return Result.error("-1", "获取店铺代码失败");
        }
        return Result.success(res);
    }

    public Result<?> getSidbyuid(@RequestBody Integer uid) {
        int res = shopMapper.getsidbyuid(uid);
        if (res == 0) {
            return Result.error("-1", "获取店铺id失败");
        }
        return Result.success(res);
    }

    public Result<?> applyShop(@RequestBody Shop shop) {
        int res = shopMapper.applyshop(shop.getSname(), shop.getScreatetime(), shop.getSpicture(), shop.getSintroduction(), shop.getSuid(), shop.getSstatus(), shop.getSsixcode(), shop.getSowner());
        if (res == 0) {
            return Result.error("-1", "申请店铺失败");
        }
        return Result.success();
    }

    public Result<?> getAllShop() {
        List<Shop> res = shopMapper.getallshop();
        if (res == null) {
            return Result.error("-1", "获取所有店铺失败");
        }
        return Result.success(res);
    }
}
